<?php
require_once 'model/Product.php';
require_once 'model/File.php';

class ProductService 
{
	private $product;
	private $file;
	
	public function __construct()
	{
		$this->product = Product::getInstance();
		$this->file = File::getInstance();
	}
	
	public function saveProduct(ProductVO $product)
	{
		$prod = $this->product->saveProduct($product);
		$product->file->idProduct = $prod->idProduct;
		$prod->file = $this->file->saveFile($product->file);
		
		return $prod;
	}
	
	public function updateProduct(ProductVO $product)
	{
		if ($product->file->content != null)
		{
			$this->file->deleteFileByProduct($product->idProduct);
			$this->file->saveFile($product->file);
		}
		
		return $this->product->updateProduct($product);
	}
	
	public function listProduct()
	{
		$products = $this->product->listProduct();
		
		if ($products != null)
			return $this->file->listFile($products);
		else 
			return null;
	}
	
	public function deleteProduct(ProductVO $product)
	{
		$this->file->deleteFileByProduct($product->idProduct);
		return $this->product->deleteProduct($product->idProduct);
	}
	
	public function searchProduct($name)
	{
		return $this->product->searchProduct($name);
	}
}